package com.prs.crm.dao.ca.work.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.ArticleTookDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.ArticleInfo;
import com.prs.crm.domain.ca.work.ArticleTook;
import com.prs.crm.domain.ca.work.ArticleTypes;

public class ArticleTookDaoImpl extends BaseDaoImpl<ArticleTook> implements
		ArticleTookDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<ArticleTook> getAllArticleTook() {
		return (List<ArticleTook>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(ArticleTook.class);
				return criteria.list();
			}
		});
	}

	@Override
	public ArticleTook getArticleTookById(Serializable id) {
		return this.getHibernateTemplate().get(ArticleTook.class, id);
	}

	@Override
	public void saveOrArticle(ArticleTook ArticleTook) {
		this.getHibernateTemplate().saveOrUpdate(ArticleTook);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArticleTook getArticleTookByBillNumber(final String atBillNumber) {
		return (ArticleTook) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(ArticleTook.class);
				criteria.add(Restrictions.eq("atBillNumber", atBillNumber));
				List<ArticleTook> article = criteria.list();
				if (article.size() == 0) {
					return null;
				} else {
					return article.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArticleTook getArticleTookBydate(final String atApplyDate) {
		return (ArticleTook) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(ArticleTook.class);
				criteria.add(Restrictions.eq("atApplyDate", atApplyDate));
				List<ArticleTook> article = criteria.list();
				if (article.size() == 0) {
					return null;
				} else {
					return article.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArticleTook getArticleTookBydateByDepartmenetId(
			final int atTookDepartmentId) {
		return (ArticleTook) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(ArticleTook.class);
				criteria.add(Restrictions.eq("atTookDepartmentId",
						atTookDepartmentId));
				List<ArticleTook> article = criteria.list();
				if (article.size() == 0) {
					return null;
				} else {
					return article.get(0);
				}
			}
		});
	}
}
